package com.sky.controller.admin;

import com.sky.result.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequiredArgsConstructor
public class CommonController {
    @PostMapping("/admin/common/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        log.info("文件上传：{}",file);
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));

        //1、定义唯一文件名
        String fileName = UUID.randomUUID() + extName;

        //2、保存到磁盘上
        file.transferTo(new File("C:\\Users\\Administrator\\Desktop\\JavaWeb\\资料\\day01\\后端初始工程\\sky-take-out\\sky-server\\src\\main\\resources\\static\\img\\"+fileName));

        //3、返回成功
        return Result.success("http://127.0.0.1:8080/img/"+fileName);
    }
}
